package com.zlsoft.web.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.lxj.xpopup.XPopup;
import com.zenglb.downloadinstaller.DownloadProgressCallBack;
import com.zlsoft.mobilenurse.R;
import com.zlsoft.web.base.BaseActivity;
import com.zlsoft.web.constant.DynamicValue;
import com.zlsoft.web.sp.ConfigPreference;
import com.zlsoft.web.utils.QrUtils;

import java.io.IOException;

/**
 * 启动
 *
 * @author - qqz
 * @date - 2021/12/20 0020/19:38
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @note -
 */

public class LaunchActivity extends BaseActivity implements DownloadProgressCallBack {

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);

        try {
            ConfigPreference.getInstance().getConfig();
        } catch (IOException mE) {
            mE.printStackTrace();
        }

        if (!DynamicValue.getWebPath().equals("")) {
            startActivityEx(MainActivity.class);
            finish();
        }

    }


    /**
     * 启动扫码
     *
     * @param view
     */
    public void startScan(View view) {
        if (DynamicValue.getWebPath().equals("")) {
            QrUtils.getInstance().QrProduct(this, QrUtils.HW_SCAN_FOR_RESULT_LOCAL_CODE);
        }
    }

    /**
     * 打开帮助
     *
     * @param view
     */
    public void openHelp(View view) {
        startActivityEx(HelpActivity.class);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == QrUtils.HW_SCAN_FOR_RESULT_LOCAL_CODE) {
            HmsScan obj = null;
            try {
                obj = data.getParcelableExtra(ScanUtil.RESULT);
            } catch (Exception e) {
                Toast.makeText(mActivity, "请重新尝试", Toast.LENGTH_SHORT).show();
            }
            if (null != obj) {
                String result = obj.originalValue;
                if (!result.contains("http")) {
                    Toast.makeText(mActivity, "该二维码不包含正确的地址信息.", Toast.LENGTH_LONG).show();
                    new XPopup.Builder(mActivity).asConfirm("扫描结果", result, "", "重新扫码", () -> {
                        startScan(getCurrentFocus());
                    }, null, true).show();
                    return;
                }
                try {

                    ConfigPreference.getInstance().setConfig(result);

                    Toast.makeText(mActivity, "配置成功", Toast.LENGTH_LONG).show();
                    startActivityEx(MainActivity.class);
                    finish();
                } catch (Exception e) {
                    Toast.makeText(mActivity, "配置失败,请参考帮助手册", Toast.LENGTH_SHORT).show();
                }


            }
        }
    }

    @Override
    public void downloadProgress(int progress) {

    }

    @Override
    public void downloadException(Exception e) {

    }

    @Override
    public void onInstallStart() {

    }
}
